<template>
  <div class="order-success-page">
    <div class="success-container">
      <div class="success-icon">
        <i class="fa fa-check-circle"></i>
      </div>
      <h2>下单成功！</h2>
      <p class="success-message">您的订单已成功提交，我们会尽快为您处理。</p>

      <div class="order-info" v-if="orderInfo">
        <div class="info-item">
          <span class="label">订单号：</span>
          <span class="value">{{ orderInfo.order_no }}</span>
        </div>
        <div class="info-item">
          <span class="label">下单时间：</span>
          <span class="value">{{ formatDate(orderInfo.create_time) }}</span>
        </div>
        <div class="info-item">
          <span class="label">应付金额：</span>
          <span class="value amount">¥{{ formatAmount(orderInfo.payment_amount) }}</span>
        </div>
      </div>

      <div class="action-buttons">
        <button @click="viewOrderDetail" class="view-order-btn">查看订单详情</button>
        <button @click="continueShopping" class="continue-btn">继续购物</button>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ordersApi, type OrderDetail, formatAmount } from '@/api/orders'

// 路由
const route = useRoute()
const router = useRouter()

// 响应式数据
const orderInfo = ref<OrderDetail | null>(null)

// 方法
const formatDate = (dateString: string) => {
  const date = new Date(dateString)
  return date.toLocaleDateString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit',
  })
}

const fetchOrderInfo = async () => {
  try {
    const orderId = Number(route.query.order_id)
    if (orderId) {
      const detail = await ordersApi.getOrderDetail(orderId)
      orderInfo.value = detail
    }
  } catch (err) {
    console.error('获取订单信息失败:', err)
  }
}

const viewOrderDetail = () => {
  if (orderInfo.value) {
    router.push(`/order/${orderInfo.value.order_id}`)
  } else {
    router.push('/orders')
  }
}

const continueShopping = () => {
  router.push('/')
}

// 生命周期
onMounted(() => {
  fetchOrderInfo()
})
</script>

<style scoped lang="scss">
.order-success-page {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-color: #f5f5f5;
  padding: 20px;

  .success-container {
    background: white;
    border-radius: 8px;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
    padding: 50px 40px;
    text-align: center;
    max-width: 500px;
    width: 100%;

    .success-icon {
      i {
        font-size: 80px;
        color: #67c23a;
        margin-bottom: 20px;
      }
    }

    h2 {
      font-size: 28px;
      color: #333;
      margin-bottom: 15px;
    }

    .success-message {
      color: #666;
      font-size: 16px;
      margin-bottom: 30px;
      line-height: 1.5;
    }

    .order-info {
      background-color: #f9f9f9;
      border-radius: 4px;
      padding: 20px;
      margin-bottom: 30px;
      text-align: left;

      .info-item {
        display: flex;
        justify-content: space-between;
        margin-bottom: 15px;

        &:last-child {
          margin-bottom: 0;
        }

        .label {
          color: #666;
        }

        .value {
          color: #333;
          font-weight: 500;

          &.amount {
            color: #f56c6c;
            font-size: 18px;
            font-weight: bold;
          }
        }
      }
    }

    .action-buttons {
      display: flex;
      gap: 15px;
      justify-content: center;

      button {
        padding: 12px 24px;
        border: none;
        border-radius: 4px;
        cursor: pointer;
        font-size: 16px;
        transition: all 0.3s;

        &.view-order-btn {
          background-color: #409eff;
          color: white;

          &:hover {
            background-color: #3080e0;
          }
        }

        &.continue-btn {
          background-color: #f5f5f5;
          color: #666;

          &:hover {
            background-color: #e6e6e6;
          }
        }
      }
    }
  }
}

@media (max-width: 768px) {
  .order-success-page {
    padding: 10px;

    .success-container {
      padding: 30px 20px;

      .action-buttons {
        flex-direction: column;

        button {
          width: 100%;
        }
      }
    }
  }
}
</style>
